package com.codeOfDoom.Sets.Player
{
	import com.codeOfDoom.Sets.Events.BadSetSelectedEvent;
	import com.codeOfDoom.Sets.Events.SetFoundEvent;
	import com.codeOfDoom.Sets.Managers.EffectsManager.EffectsManager;
	import com.codeOfDoom.Sets.Managers.SetsManager.SetsManager;
	import com.codeOfDoom.Sets.SetsCard.SetsCard;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;

	public class Player extends EventDispatcher implements IPlayer
	{
		private var selectedCards:ArrayCollection;
		
		public function Player()		
		{
			selectedCards = new ArrayCollection();
		}

		public function announceSet():void
		{
			var isSet:Boolean = SetsManager.verify(selectedCards);
			if (isSet){
				dispatchEvent(new SetFoundEvent(selectedCards));
				selectedCards = new ArrayCollection();
			}else{
				dispatchEvent(new BadSetSelectedEvent());
				//remove last card selected
				selectedCards.removeItemAt(2);
				
			}
		}
		
		public function clearCards():void{
			for each (var setsCard:SetsCard in selectedCards){
				setsCard.selected= false;
			} 
			dropCards();
		}
		
		public function dropCards():void{
			selectedCards = new ArrayCollection();
		}
		public function selectCard(setsCard:SetsCard):void{						
			//checks to see if its already been selected. if it is, it deselects it. if not, it adds it.
			selectedCards.contains(setsCard)?
				selectedCards.removeItemAt(selectedCards.getItemIndex(setsCard))
				:selectedCards.addItem(setsCard);
				
			//sets color to card. No need to add the effect to the 3rd card. 
			//if it is a good set, then the card will disappear. if it is a bad set, then 
			//the card will not be selected.
			if (selectedCards.length < 3)
				setsCard.selected =selectedCards.contains(setsCard);
			if (selectedCards.length == 3)
				announceSet();
		}
		
	}
}